﻿CREATE OR REPLACE FUNCTION "DropFunction_AWIL"(functionname text)
  RETURNS text AS
$BODY$
DECLARE
    funcrow RECORD;
    numfunctions smallint := 0;
    numparameters int;
    i int;
    paramtext text;
BEGIN

if functionname='rcrsvcln' THEN
	FOR funcrow IN SELECT proargtypes,proname FROM pg_proc WHERE proname LIKE 'Func%' LOOP

	    --for some reason array_upper is off by one for the oidvector type, hence the +1
	    numparameters = array_upper(funcrow.proargtypes, 1) + 1;

	    i = 0;
	    paramtext = '';

	    LOOP
		IF i < numparameters THEN
		    IF i > 0 THEN
			paramtext = paramtext || ', ';
		    END IF;
		    paramtext = paramtext || (SELECT typname FROM pg_type WHERE oid = funcrow.proargtypes[i]);
		    i = i + 1;
		ELSE
		    EXIT;
		END IF;
	    END LOOP;

	    EXECUTE 'DROP FUNCTION "' || CAST(funcrow.proname as VARCHAR(128)) || '"(' || paramtext || ');';
	    numfunctions = numfunctions + 1;
	END LOOP;
	EXECUTE 'DROP FUNCTION "DropFunction_AWIL"(functionname text);';
ELSE
	FOR funcrow IN SELECT proargtypes FROM pg_proc WHERE proname = functionname LOOP

	    --for some reason array_upper is off by one for the oidvector type, hence the +1
	    numparameters = array_upper(funcrow.proargtypes, 1) + 1;

	    i = 0;
	    paramtext = '';

	    LOOP
		IF i < numparameters THEN
		    IF i > 0 THEN
			paramtext = paramtext || ', ';
		    END IF;
		    paramtext = paramtext || (SELECT typname FROM pg_type WHERE oid = funcrow.proargtypes[i]);
		    i = i + 1;
		ELSE
		    EXIT;
		END IF;
	    END LOOP;

	    EXECUTE 'DROP FUNCTION "' || functionname || '"(' || paramtext || ');';
	    numfunctions = numfunctions + 1;
	END LOOP;
END IF;

RETURN 'se eliminaron ' || numfunctions || ' functions';
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;